package org.jsola.hr.service.impl;

import org.jsola.core.Page;
import org.jsola.hr.common.ExampleKit;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dao.INewsFileDAO;
import org.jsola.hr.dto.NewsFileAddDTO;
import org.jsola.hr.dto.NewsFileUpdateDTO;
import org.jsola.hr.entity.NewsFileDO;
import org.jsola.hr.query.NewsFileQuery;
import org.jsola.hr.service.INewsFileService;
import org.jsola.hr.vo.NewsFileListVO;
import org.jsola.hr.vo.NewsFileVO;
import org.jsola.user.core.TokenUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 官网新闻内容附带的附件
 *
 * @author zhr
 */
@Service("hrNewsFileServiceImpl")
public class NewsFileServiceImpl implements INewsFileService {

    @Autowired
    private INewsFileDAO newsFileDAO;

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public NewsFileVO save(NewsFileAddDTO newsFileAddDTO, TokenUser tokenUser) {
        // 转DO
        NewsFileDO newsFileDO = newsFileAddDTO.to(NewsFileDO.class);
        // 保存
        newsFileDO = save(newsFileDO, tokenUser.getSiteId(), tokenUser.getUserId());
        return newsFileDO.to(NewsFileVO.class);
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public int update(NewsFileUpdateDTO newsFileUpdateDTO, TokenUser tokenUser) {
        //转DO
        NewsFileDO newsFileDO = newsFileUpdateDTO.to(NewsFileDO.class);
        //根据主键更新，只更新非null值
        return updateByIdSelective(newsFileDO, tokenUser.getSiteId(), tokenUser.getUserId());
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public int deleteByIds(TokenUser tokenUser, String... newsFileIds) {
        return logicalDeleteByIds(tokenUser.getSiteId(), tokenUser.getUserId(), (Object[]) newsFileIds);
    }


    @Override
    public NewsFileVO selectById(String newsFileId, String siteId) {
        NewsFileDO newsFileDO = selectDOById(newsFileId, siteId);
        if (newsFileDO == null) {
            return null;
        }
        return newsFileDO.to(NewsFileVO.class);
    }

    @Override
    public List<NewsFileListVO> select(NewsFileQuery newsFileQuery, String siteId) {
        List<NewsFileDO> newsFileDOList = selectDO(newsFileQuery, siteId);
        if(CollectionUtils.isEmpty(newsFileDOList)) {
            return newsFileDOList == null ? null : new ArrayList<>();
        }
        return newsFileDOList.stream()
                .map(newsFileDO -> newsFileDO.to(NewsFileListVO.class))
                .collect(Collectors.toList());
    }

    @Override
    public int selectCount(NewsFileQuery newsFileQuery, String siteId) {
        Example example = buildExample(newsFileQuery,siteId);
        return newsFileDAO.selectCountByExample(example);
    }

    @Override
    public Page<NewsFileListVO> selectPage(NewsFileQuery newsFileQuery, String siteId) {
        Example example = buildExample(newsFileQuery,siteId);
        Page<NewsFileDO> page = newsFileDAO.selectPageByExample(example,
                newsFileQuery.getPageNo(),
                newsFileQuery.getPageSize());

        return page.to(NewsFileListVO.class);
    }

    @Override
    public NewsFileDO selectDOById(String newsFileId, String siteId) {
        return listById(newsFileId, siteId);
    }

    @Override
    public List<NewsFileDO> selectDO(NewsFileQuery newsFileQuery, String siteId) {
        Example example = buildExample(newsFileQuery,siteId);
        return newsFileDAO.selectByExample(example);
    }


    /**
     * 根据查询参数，构建example

     * @param newsFileQuery 查询参数
     * @param siteId 所属站点id
     * @return example
     */
    private Example buildExample(NewsFileQuery newsFileQuery, String siteId) {
        Example example = new Example(NewsFileDO.class);
        example.and()
                .andEqualTo("valid", true)
                .andEqualTo("siteId", siteId);
        if (newsFileQuery.getSort() != null) {
            example.and().andEqualTo("sort", newsFileQuery.getSort());
        }
        if (newsFileQuery.getNewsInfoId() != null) {
            example.and().andEqualTo("newsInfoId", newsFileQuery.getNewsInfoId());
        }
        if (newsFileQuery.getUrl() != null) {
            example.and().andEqualTo("url", newsFileQuery.getUrl());
        }
        if (newsFileQuery.getName() != null) {
            example.and().andEqualTo("name", newsFileQuery.getName());
        }
        // 排序
        ExampleKit.setExampleOrder(example,newsFileQuery.getOrders());
        return example;
    }
}




